<template>
  <!-- 基础题库 -->
  <div class='dashboard-container'>
    <keep-alive>
      <question-bank :list="questionsList"
      :pagination="pagination"
      :counts="counts"
      :titleDetails="titleDetails"
      @delTitle="delTitle"
      @addBank="addBank"
      @preview="preview"
      @pageChange="pageChange"
      @searchTopic="searchTopic"
      />
    </keep-alive>
  </div>
</template>

<script>
import questionBank from '../components/question-bank.vue'
import { list, remove, choiceAdd, detail } from '@/api/hmmm/questions'
export default {
  data () {
    return {
      // 题目列表
      questionsList: [],
      counts: 0,
      pagination: {
        // 当前页
        page: 1,
        // 当前页显示总数
        pagesize: 10
      },
      // pages: 1,
      titleDetails: {}
    }
  },
  components: {
    questionBank
  },
  methods: {
    // 获取问题列表
    async getQuestionsList (data) {
      const { data: { counts, items, page, pages, pagesize } } = await list(data)
      this.counts = counts
      this.questionsList = items
      this.pagination.page = page
      this.pagination.pages = pages
      this.pagination.pagesize = pagesize
    },
    // 删除题目
    delTitle (data) {
      this.$confirm('此操作将永久删除该题目，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: { success } } = await remove(data)
        if (!success) return this.$message.error('删除基础失败')
        this.$message.success('删除基础成功')
        this.getQuestionsList()
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    // 加入精选题库
    addBank (data) {
      this.$confirm('此操作将该题目加入精选，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async () => {
        const { data: { success } } = await choiceAdd(data)
        if (!success) return this.$message.error('进入精选失败')
        this.$message.success('加入精选成功')
        this.getQuestionsList()
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    // 预览弹层
    async preview (row) {
      const { data } = await detail(row)
      this.titleDetails = data
    },
    pageChange (data) {
      this.getQuestionsList(data)
    },
    // 根据学科搜索
    searchTopic (data) {
      if (!data?.subjectID) return this.getQuestionsList()
      this.getQuestionsList(data)
    }
  },
  created () {
    this.getQuestionsList(this.pagination)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .dashboard-container {
  padding: 20px;
}
</style>
